<?php
// ini_set('display_error', 0);
// error_reporting(0);
ini_set('max_execution_time','0');
//include_once 'Excel.php';
include_once 'init.php';


final class Index{
    private $filePath = '';
    public function __construct()
    {
        $functionName = file_get_contents("php://input");//$_REQUEST;//isset($_REQUEST['func']) ? $_REQUEST['func'] : '';
        $arr = json_decode($functionName, true);
         if (!$arr){
             $arr = $_REQUEST;
         }
        $functionName = $arr['func'];
        try {
            $res = $this->$functionName($arr);
            echo $res;
        }catch (ExceptionNew $e){
            throw new ExceptionNew($functionName);
        }
       
    }
    
    public function run($param){
          $luck = new luck();
          $user = $luck->getUser($param['name']);

          $Data = [
              'user_id' => $user['id'],
              'user_name' => $user['name']
          ];
          $response = [];
          if ($user){
              $response = $luck->getAssessor();
              $response = array_column($response, 'assessor_name');
//              $response['first_Assessor_name'][0] = $luck->getAssessorById($user['first_Assessor_one_id'])['assessor_name'];
//              $response['first_Assessor_name'][1] = $luck->getAssessorById($user['first_Assessor_two_id'])['assessor_name'];
//              $response['first_Assessor_name'][2] = $luck->getAssessorById($user['first_Assessor_three_id'])['assessor_name'];
//              $response['second_Assessor_name'][0] = $luck->getAssessorById($user['second_Assessor_one_id'])['assessor_name'];
//              $response['second_Assessor_name'][1] = $luck->getAssessorById($user['second_Assessor_two_id'])['assessor_name'];
//              $response['second_Assessor_name'][2] = $luck->getAssessorById($user['second_Assessor_three_id'])['assessor_name'];
//              $response['second_Assessor_name'][3] = $luck->getAssessorById($user['second_Assessor_four_id'])['assessor_name'];
          }

          $luck->UpDataUserQueue($Data);
          echo json_encode($response);
    }

    /**
     * 获取当前汇报者的评委评定情况
     * @time 2020/12/16 15:43
     * @return void
     */
    public function assessok($param){
        $list = [];
        if (isset($param['name']) && $param['name']){
            $luck = new luck();
            $list = $luck->getUserScoreByUserName($param['name']);
            $list = array_column($list, 'Assessor_name', 'Assessor_id');
        }

        echo json_encode($list);
        return;

    }

    /**
     * 登陆
     * @time 2020/12/16 15:43
     */
    public function login()
    {
        include 'login.html';
        return;
    }

    /**
     * 登陆
     * @time 2020/12/16 15:43
     * @param $param
     */
    public function loginsave($param)
    {
        $luck = new luck();
        $assessorInfo = $luck->getAssessorAccount($param['mobile']);

        if (!$param['mobile']){
            include 'login.html';
            return;
        }
        if ($assessorInfo && $assessorInfo['password'] == $param['password'] ){
            include 'score.html';
        }else{
            include 'login.html';
        }
        return;
    }

    /**
     * 评分
     * @time 2020/12/16 15:42
     * @param $param
     */
    public function score($param)
    {

        $insertData = [
            'user_id' => $param['user_id'],
            'user_name' => $param['user_name'],
            'Assessor_id' => $param['Assessor_id'],
            'Assessor_name' => $param['Assessor_name'],
            'original_score' => isset($param['score']) ? round(array_sum($param['score']), 1) : 0,
        ];

        $luck = new luck();
        $scoreInfo = $luck->getUserScoreByUserIdAndAssessorId($insertData['user_id'], $insertData['Assessor_id']);
        if ($scoreInfo){
            echo json_encode(['code' => '500', 'message' => '您已经对此汇报者有过评分，请勿重复评分~']);
            return;
        }
//        $select = [
//            'first_Assessor_one_id',
//            'first_Assessor_two_id',
//            'first_Assessor_three_id',
//            'second_Assessor_one_id',
//            'second_Assessor_two_id',
//            'second_Assessor_three_id',
//            'second_Assessor_four_id'
//        ];
//        $select = implode(',', $select);
//        $userInfo = $luck->getUserById($param['user_id'], $select);
//        $info = array_values($userInfo);
//        $idKey = array_search($param['Assessor_id'],$info);
//
//        $score = 0;
//        if ($idKey === 0){
//            $xiShu = 0.4;
//        }
//        if ($idKey > 0 && $idKey < 3){
//            $xiShu = 0.2;
//        }
//        if ($idKey > 2){
//            $xiShu = 0.05;
//        }


        $assessorInfo = $luck->getAssessorById($insertData['Assessor_id']);
        $score = 0;
        $xiShu = $assessorInfo['first'];
        $insertData['proportion'] = $xiShu;
        foreach ($param['score'] as $key => $value){
            $tmp = 0;
            switch ($key){
                case 0:
                    $tmp = $insertData['first'] = round(bcdiv($value, 100, 4) * (5 * $xiShu), 2);
                    $insertData['original_first'] = $value;
                    break;
                case 1:
                    $tmp = $insertData['second'] = round(bcdiv($value, 100, 4) * (5 * $xiShu), 2);
                    $insertData['original_second'] = $value;
                    break;
                case 2:
                    $tmp = $insertData['third'] = round(bcdiv($value, 100, 4) * (10 * $xiShu), 2);
                    $insertData['original_third'] = $value;
                    break;
                case 3:
                    $tmp = $insertData['fourth'] = round(bcdiv($value, 100, 4) * (20 * $xiShu), 2);
                    $insertData['original_fourth'] = $value;
                    break;
                case 4:
                    $tmp = $insertData['fifth'] = round(bcdiv($value, 100, 4) * (20 * $xiShu), 2);
                    $insertData['original_fifth'] = $value;
                    break;
                case 5:
                    $tmp = $insertData['sixth'] = round(bcdiv($value, 100, 4) * (20 * $xiShu), 2);
                    $insertData['original_sixth'] = $value;
                    break;
                case 6:
                    $tmp = $insertData['seventh'] = round(bcdiv($value, 100, 4) * (20 * $xiShu), 2);
                    $insertData['original_seventh'] = $value;
                    break;
            }
            $score +=$tmp;
        }

        $insertData['score'] = $score;
        $res = $luck->insertScore($insertData);
        if ($res){
            echo json_encode(['code' => '200', 'message' => '评分成功~']);
        }else{
            echo json_encode(['code' => '500', 'message' => '评分失败~']);
        }
        return;
    }

    /**
     * 获取当前评委的打分记录
     * @time 2020/12/16 15:42
     * @param $param
     */
    public function userscore($param)
    {
        $luck = new luck();
        $assessorInfo = $luck->getAssessorById($param['aid']);
        $list = $luck->getUserScoreByAssessorId($param['aid']);
        $list1 = $list;

        array_multisort(array_column($list1, 'score'),SORT_DESC, $list1);
        $tmpValue = array_column($list1, 'id');
        foreach ($list as &$value){
            foreach ($tmpValue as $key => $tmp){
                if ($value['id'] == $tmp){
                    $value['scoreSort'] = $key+1;
                }
            }
        }
//        pre($list);
        include 'list.html';
    }

    /**
     * 导航页
     * link
     * @return void
     * USER zengqilong
     * 2024/1/18 9:37
     */
    public function link()
    {
        include 'link.html';
    }

    public function ranking($param)
    {
        $name = !empty($param['name'])?$param['name']:'排行榜';
        $list = [];
        $luck = new luck();
        switch ($name) {
            case '汇报总分排名':
                $list = $luck->getUserScore();
                $ImgName = 1;
                break;
            case '团队平均分排名':
                $list = $luck->getUserScoreGroup();
                $ImgName = 2;
                break;
            case '个人风采分排名':
                $ImgName = 3;
                $list = $luck->getUserScoreProfile();
                break;
        }
        $data = [];
        foreach ($list as $key => $value){
            switch ($name) {
                case '汇报总分排名':
                    $data[$key]['name'] = $value['user_name'];
                    $data[$key]['score'] = number_format($value['score'], 2, '.', 0);
                    break;
                case '团队平均分排名':
                    $data[$key]['name'] = $value['group'];
                    $data[$key]['score'] = number_format($value['score'], 2, '.', 0);
                    break;
                case '个人风采分排名':
                    $data[$key]['name'] = $value['user_name'];
                    $data[$key]['score'] = number_format($value['sumScore'], 2, '.', 0);
                    break;
            }
        }

        include 'ranking.html';
    }
    /**
     * 获取当前评委的打分记录
     * @time 2020/12/16 15:42
     * @param $param
     */
    public function userscorelist($param)
    {
        $luck = new luck();
        $list = $luck->getUserNotInScore($param['aid']);

        include 'listall.html';
    }

    /**
     * 获取修改者的分数信息
     * @time 2020/12/16 13:56
     * @param $param
     */
    public function edit($param)
    {
        $luck = new luck();
        $info = $luck->getUserScoreById($param['id']);
        echo json_encode(['code' => '200', 'message' => '', 'data' => $info]);
        return;
    }

    /**
     * 获取修改者的分数信息
     * @time 2020/12/16 13:56
     * @param $param
     */
    public function create($param)
    {
        $luck = new luck();
        $select = implode(',', ['id', 'name']);
        $info = $luck->getUserById($param['id'], $select);
        echo json_encode(['code' => '200', 'message' => '', 'data' => $info]);
        return;
    }

    /**
     * 修改分数
     * @time 2020/12/16 13:56
     */
    public function editsave($param)
    {
        $luck = new luck();
        $info = $luck->getUserScoreById($param['id']);
        $update = [
            'original_score' => array_sum($param['score']),
            'original_first' => $param['score'][0],
            'original_second' => $param['score'][1],
            'original_third' => $param['score'][2],
            'original_fourth' => $param['score'][3],
            'original_fifth' => $param['score'][4],
            'original_sixth' => $param['score'][5],
            'original_seventh' => $param['score'][6],
            'first' => round(bcdiv($param['score'][0], 100, 4) * (5 * $info['proportion']), 2),
            'second' => round(bcdiv($param['score'][1], 100, 4) * (5 * $info['proportion']), 2),
            'third' => round(bcdiv($param['score'][2], 100, 4) * (10 * $info['proportion']), 2),
            'fourth' => round(bcdiv($param['score'][3], 100, 4) * (20 * $info['proportion']), 2),
            'fifth' => round(bcdiv($param['score'][4], 100, 4) * (20 * $info['proportion']), 2),
            'sixth' => round(bcdiv($param['score'][5], 100, 4) * (20 * $info['proportion']), 2),
            'seventh' => round(bcdiv($param['score'][6], 100, 4) * (20 * $info['proportion']), 2),
        ];
       
        $update['score'] = $update['first'] + $update['second'] + $update['third'] + $update['fourth'] + $update['fifth'] + $update['sixth'] + $update['seventh'];

        $res = $luck->UpdateScoreById($param['id'], $update);
        if ($res !== false){
            echo json_encode(['code' => '200', 'message' => '操作成功']);
        }else{
            echo json_encode(['code' => '500', 'message' => '操作失败']);
        }
        return;
    }

    /**
     * 获取正在进行的汇报者
     * @time 2020/12/12 10:27
     * @param $arr
     */
    public function user($arr)
    {
        $luck = new luck();
        $info = $luck->getUserQueue();
        $isOk = $luck->getIsOkUser();
        $userCount = $luck->getUserCount();
        $info['pep'] = ($isOk['countId'] ? $isOk['countId'] : 0).'/'.($userCount['countId'] ? $userCount['countId'] : 0);
        echo json_encode($info);return;
    }

    public function analysis()
    {
        $luck = new luck();
        $analysis = $luck->getUserScore();
        foreach ($analysis as $analysisKey => &$analysisVal){
            $analysisVal['score'] = round($analysisVal['score'], 2);
            $analysisVal['first'] = round($analysisVal['first'], 2);
            $analysisVal['second'] = round($analysisVal['second'], 2);
            $analysisVal['third'] = round($analysisVal['third'], 2);
        }

        include "analysis.html";
    }

    /**
     * 上传文件
     */
    public function uploadFile()
    {
        $file = $_FILES['files'];
        $ext = explode('.', $file['name']);
        $this->filePath = './file/'.time().'.'.$ext[1];
        $res = $this->wp_move_uploaded_file($file['tmp_name'], './file/'.time().'.'.$ext[1]);
        if ($res){
            $excel = new excel();
            $count = $excel->getExcel($this->filePath);
           if ($count){
               return 200;
           }
        }
        return 0;
    }
    
    private function wp_move_uploaded_file( $filename, $destination ) {
        
        if(move_uploaded_file($filename,$destination)) {
            return true;
        } else {
            return false;
        }
    }

    public function getUser()
    {
        $luck = new luck();
        $user = $luck->getUserList();
        $names = array_column($user, 'name');
        echo json_encode(['code' => '200', 'message' => '操作成功', 'data' => $names]);
    }
     
}

new Index();

